package com.gopay.common.rcs.service.antiphish.manager.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.rcs.AntiPhishCheckOrderIpEnum;
import com.gopay.common.constants.rcs.AntiPhishEditNatureEnum;
import com.gopay.common.constants.rcs.AntiPhishGetwayChannelEnum;
import com.gopay.common.domain.rcs.RcsAntiPhishConfCust;
import com.gopay.common.domain.rcs.RcsAntiPhishConfEditLog;
import com.gopay.common.rcs.manager.antiphish.RcsAntiPhishingCustManager;
import com.gopay.common.rcs.service.antiphish.manager.RcsAntiPhishingManagerCustService;
import com.gopay.common.rcs.vo.RcsAntiPhishConfCustlVo;
import com.gopay.common.util.StringTools;

/**
 * 商户防钓鱼配置：修改、查询
 * 
 * @RcsAntiPhishingManagerCustService.java
 * @author liujy
 * @2013-7-22 下午2:40:07 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("rcsAntiPhishingManagerCustService")
public class RcsAntiPhishingManagerCustServiceImpl implements RcsAntiPhishingManagerCustService {
	private static Object lock = new Object();
    @Resource(name = "rcsAntiPhishingCustManager")
    RcsAntiPhishingCustManager rcsAntiPhishingCustManager;


    @Override
    public int getCount(String custName, String email, String custMob) {
        return rcsAntiPhishingCustManager.getCount(custName, email, custMob);
    }

    /**
     * 
     * 商户初始化 数据
     * 
     * @return
     */
    List<RcsAntiPhishConfCust> initailConfList(RcsAntiPhishConfCustlVo newCustConf) {

        List<RcsAntiPhishConfCust> custConfList = new ArrayList<RcsAntiPhishConfCust>();
        /**
         * 商户1的 精确域名匹配
         */
        initCheckDomain(newCustConf, custConfList);
        /**
         * 商户1的 模糊域名匹配
         */
        initCheckLikeDomain(newCustConf, custConfList);
        /**
         * 商户1的 时间戳间隔校验
         */
        initChckTime(newCustConf, custConfList);
        /**
         * 商户1的 下单及支付IP比对校验
         */
        initCheckOderIp(newCustConf, custConfList);
        /**
         * 商户1的 验证码输入
         */
        initEnterValidity(newCustConf, custConfList);
        /**
         * 商户1的 交易请求源IP校验
         */
        initReqSource(newCustConf, custConfList);

        return custConfList;
    }

    private void initReqSource(RcsAntiPhishConfCustlVo newCustConf,
            List<RcsAntiPhishConfCust> custConfList) {
        RcsAntiPhishConfCust antiPhishConfCust16 = new RcsAntiPhishConfCust();
        antiPhishConfCust16.setCustId(newCustConf.getCustId());
        antiPhishConfCust16.setAntiPhishType("06");
        antiPhishConfCust16.setGatewayChannel("03");
        antiPhishConfCust16.setIsEnabled(StringUtils.isNotBlank(newCustConf.getReqSourceIp()) ? 1 : 0);
        antiPhishConfCust16.setIllegalReqDicision("01");
        antiPhishConfCust16.setLegalValue(StringTools.checkNulltoString(newCustConf.getReqSourceIp()));
        custConfList.add(antiPhishConfCust16);
    }

    private void initEnterValidity(RcsAntiPhishConfCustlVo newCustConf,
            List<RcsAntiPhishConfCust> custConfList) {
        RcsAntiPhishConfCust antiPhishConfCust15 = new RcsAntiPhishConfCust();
        antiPhishConfCust15.setCustId(newCustConf.getCustId());
        antiPhishConfCust15.setAntiPhishType("05");
        antiPhishConfCust15.setGatewayChannel("01");
        antiPhishConfCust15.setIllegalReqDicision("01");
        // 设置logo
        if (StringUtils.isNotBlank(newCustConf.getUploadLogo())) {
            antiPhishConfCust15.setLegalValue(StringTools.checkNulltoString(newCustConf.getUploadLogo()));
        }
        if (newCustConf.getEnterValidityCode().equals(AntiPhishGetwayChannelEnum.ALL_CLOSE.value)) {
            antiPhishConfCust15.setIsEnabled(0);
        } else  {
            antiPhishConfCust15.setIsEnabled(1);
            antiPhishConfCust15.setGatewayChannel(newCustConf.getEnterValidityCode());
        }
        custConfList.add(antiPhishConfCust15);
    }

    private void initChckTime(RcsAntiPhishConfCustlVo newCustConf,
            List<RcsAntiPhishConfCust> custConfList) {

        RcsAntiPhishConfCust antiPhishConfCust14 = new RcsAntiPhishConfCust();
        antiPhishConfCust14.setCustId(newCustConf.getCustId());
        antiPhishConfCust14.setAntiPhishType("03");
        antiPhishConfCust14.setGatewayChannel("01");
        antiPhishConfCust14.setIsEnabled(Integer.parseInt(newCustConf.getCheckTimeStamp()));
        antiPhishConfCust14.setIllegalReqDicision("01");
        antiPhishConfCust14.setLegalValue("45");    //永远是45秒

        custConfList.add(antiPhishConfCust14);
    }

    private void initCheckOderIp(RcsAntiPhishConfCustlVo newCustConf,
            List<RcsAntiPhishConfCust> custConfList) {

        RcsAntiPhishConfCust antiPhishConfCust13 = new RcsAntiPhishConfCust();
        antiPhishConfCust13.setCustId(newCustConf.getCustId());
        antiPhishConfCust13.setGatewayChannel("01");
        antiPhishConfCust13.setAntiPhishType("04");
        antiPhishConfCust13.setIllegalReqDicision("01");

        if (newCustConf.getCheckOrderIP().equals(AntiPhishCheckOrderIpEnum.CLAOSE.value)) {
            antiPhishConfCust13.setIsEnabled(0);
        } else if (newCustConf.getCheckOrderIP().equals(AntiPhishCheckOrderIpEnum.REJECT.value)) {
            antiPhishConfCust13.setIsEnabled(1);
        } else if (newCustConf.getCheckOrderIP().equals(AntiPhishCheckOrderIpEnum.WARN.value)) {
            antiPhishConfCust13.setIsEnabled(1);
            antiPhishConfCust13.setIllegalReqDicision("02");
        }
        custConfList.add(antiPhishConfCust13);
    }

    private void initCheckDomain(RcsAntiPhishConfCustlVo newCustConf,
            List<RcsAntiPhishConfCust> custConfList) {
        RcsAntiPhishConfCust antiPhishConfCust1 = new RcsAntiPhishConfCust();
        antiPhishConfCust1.setCustId(newCustConf.getCustId());
        antiPhishConfCust1.setGatewayChannel("01");
        antiPhishConfCust1.setIllegalReqDicision("01");
        // 精确开启或者关闭
        antiPhishConfCust1.setAntiPhishType("01");
        antiPhishConfCust1.setIsEnabled(Integer.parseInt(newCustConf.getCheckDomain()));
        antiPhishConfCust1.setLegalValue(newCustConf.getCheckDomainValue());
        custConfList.add(antiPhishConfCust1);
    }
    private void initCheckLikeDomain(RcsAntiPhishConfCustlVo newCustConf,
            List<RcsAntiPhishConfCust> custConfList) {
        RcsAntiPhishConfCust antiPhishConfCust1 = new RcsAntiPhishConfCust();
        antiPhishConfCust1.setCustId(newCustConf.getCustId());
        antiPhishConfCust1.setGatewayChannel("01");
        antiPhishConfCust1.setIllegalReqDicision("01");
        // 模糊开启或者关闭
        antiPhishConfCust1.setAntiPhishType("02");
        antiPhishConfCust1.setIsEnabled(Integer.parseInt(newCustConf.getCheckLikeDomain()));
        antiPhishConfCust1.setLegalValue(newCustConf.getCheckDomainLikeValue());//生产bug8期 lx 2017-8-11 17:17:33 修复设置初始化值 取控制台输入值
        custConfList.add(antiPhishConfCust1);
    }
    


    /*
     * 查询列表
     * 
     * @see com.gopay.common.rcs.service.antiphish.manager.
     * RcsAntiPhishingManagerCustService#getCustConfListBySql(java.lang.String,
     * java.lang.String, int, int)
     */
    @Override
    public List<RcsAntiPhishConfCustlVo> getCustConfListBySql(String custName, String email, String custMob, int pageNo, int pageSize) {
        List<RcsAntiPhishConfCustlVo> confVoList = new ArrayList<RcsAntiPhishConfCustlVo>();
        List<?> objList = rcsAntiPhishingCustManager.getCustConfBySqlList(custName, email, custMob, pageNo, pageSize);
        buildVoList(confVoList, objList);
        return confVoList;
    }

    private void buildVoList(List<RcsAntiPhishConfCustlVo> confVoList, List<?> objList) {
        boolean isSetBaseInfo = false;
        int index = 0;
        RcsAntiPhishConfCustlVo confVo = null;
        for (int i = 0, size = null == objList ? 0 : objList.size(); i < size; i++) {
        	Object[] propertyArray = (Object[])objList.get(i);
            if (index == 0 || index % 6 == 0) {// 每个商户6条数据
                confVo = new RcsAntiPhishConfCustlVo();
                isSetBaseInfo = false;
            }

            // 设置基本公共信息
            if (!isSetBaseInfo) {
                confVo.setCustEmail((String) propertyArray[5]);
                confVo.setCustMob((String) propertyArray[6]);
                confVo.setCustName((String) propertyArray[7]);
                confVo.setSiteName((String) propertyArray[8]);
                confVo.setCustId((String) propertyArray[9]);
            }
            isSetBaseInfo = true;
            // 校验类型
            String antiPhishType = (String) propertyArray[0];
            // 开关
            String isEnabled = ((BigDecimal) propertyArray[1]).toString();

            if (antiPhishType.equals("01") || antiPhishType.equals("02")) {
                if (antiPhishType.equals("02")) {
                    confVo.setCheckLikeDomain(isEnabled);
                    // 精确域名校验
                } else {
                    confVo.setCheckDomain(isEnabled);
                }
            } else if (antiPhishType.equals("03")) {
                // 时间戳间隔校验
                confVo.setCheckTimeStamp(isEnabled);
                confVo.setLegalValue((String) propertyArray[2]);
            } else if (antiPhishType.equals("04")) {
                // 订单IP校验
                confVo.setCheckOrderIP(getCheckOrderIPVal((String) propertyArray[3], isEnabled));
            } else if (antiPhishType.equals("05")) {
                // 验证码输入
                String gatewayChannel = (String) propertyArray[4];

                if(isEnabled.equals("0")){
                    gatewayChannel=AntiPhishGetwayChannelEnum.ALL_CLOSE.value;
                }
                confVo.setEnterValidityCode(gatewayChannel);
            } else if (antiPhishType.equals("06")) {
                confVo.setReqSourceIp((String) propertyArray[2]);
            }
            if (index % 6 == 0) {// 每个商户6条数据

                confVoList.add(confVo);
            }
            index++;
        }

    }

    /*
     * 更新配置信息
     * 
     * @see com.gopay.common.rcs.service.antiphish.manager.
     * RcsAntiPhishingManagerCustService
     * #updateCustConf(com.gopay.common.rcs.vo.RcsAntiPhishConfCustlVo)
     */
    @Override
	public void updateCustConf(RcsAntiPhishConfCustlVo newCustConf) {

//		List<?> strCustConf = rcsAntiPhishingCustManager.getCustConfBySql(newCustConf.getCustEmail());//2015 edit bug

        synchronized (lock) {
            List<?> strCustConf = rcsAntiPhishingCustManager.getCustConfBySql(newCustConf.getCustId());
            // 如果商户信息未初始化
            if (null == strCustConf || strCustConf.isEmpty()) {
                List<RcsAntiPhishConfCust> list = initailConfList(newCustConf);
                rcsAntiPhishingCustManager.saveCustConf(newCustConf, list, newCustConf.getOperId(), newCustConf.getOperName());
                return;
            }
            RcsAntiPhishConfCustlVo oldCustConf = new RcsAntiPhishConfCustlVo();
            buildVo(oldCustConf, strCustConf);

            List<RcsAntiPhishConfCust> list = new ArrayList<RcsAntiPhishConfCust>();
            List<RcsAntiPhishConfEditLog> logList = new ArrayList<RcsAntiPhishConfEditLog>();

            buildUpdateList(newCustConf, list, oldCustConf, logList);
            rcsAntiPhishingCustManager.updateCustConf(list, logList);
        }

	}

    /**
     * 构建需要更新的规则对象 list
     * 
     * @param newCustVo
     * @param list
     * @param oldCustVo
     */
    private void buildUpdateList(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {
    	
        // 域名是否发送改变
        setDomainChange(newCustVo, list, oldCustVo,logList);
        // 模糊域名是否发送改变
        setDomainLikeChange(newCustVo, list, oldCustVo,logList);
        // 时间戳是校验否修改
        setTimeStampChange(newCustVo, list, oldCustVo,logList);
        // 订单IP校验否修改
        setCheckOrderIPChange(newCustVo, list, oldCustVo,logList);
        // 验证码输入校验否修改
        setEnterValidityCodeChange(newCustVo, list, oldCustVo,logList);
        // 请求源发生改变
        setReqSourceIPChange(newCustVo, list, oldCustVo,logList);
    }

    /**
     * 设置请求源发生更新参数
     * 
     * @param newCustVo
     * @param list
     * @param oldCustVo
     */
    private void setReqSourceIPChange(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {
        String newReqSourceIp = StringTools.checkNulltoString(newCustVo.getReqSourceIp());
        String oldReqSourceIp = StringTools.checkNulltoString(oldCustVo.getReqSourceIp());
        // 请求源IP校验否修改
        if (!newReqSourceIp.equals(oldReqSourceIp)) {
            RcsAntiPhishConfCust checkReqSourceIp = new RcsAntiPhishConfCust();
            checkReqSourceIp.setConfSq(Integer.parseInt(oldCustVo.getReqSourceIpId()));
            checkReqSourceIp.setAntiPhishType("06");
            checkReqSourceIp.setLegalValue(newCustVo.getReqSourceIp());
            if (newReqSourceIp.equals("")) {
                checkReqSourceIp.setIsEnabled(0);
            } else {
                checkReqSourceIp.setIsEnabled(1);
            }
            list.add(checkReqSourceIp);
            
            //记日志
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getReqSourceIpId(), newCustVo);
            log.setValBeforeEdit(oldReqSourceIp);
            log.setValAfterEdit(newReqSourceIp);
            log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
            logList.add(log);
        }
    }

    private void setEnterValidityCodeChange(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {
        if (!newCustVo.getEnterValidityCode().equals(oldCustVo.getEnterValidityCode())
                || !StringTools.checkNulltoString(oldCustVo.getUploadLogo()).equals(
                        StringTools.checkNulltoString(newCustVo.getUploadLogo()))) {
            setEnterValidityCode(newCustVo, list,oldCustVo,logList);
        }
    }

    private void setCheckOrderIPChange(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {
        if (!newCustVo.getCheckOrderIP().equals(oldCustVo.getCheckOrderIP())) {
            RcsAntiPhishConfCust checkIP = new RcsAntiPhishConfCust();
            checkIP.setConfSq(Integer.parseInt(oldCustVo.getOrderIpId()));
            checkIP.setAntiPhishType("04");
            // 开关是否变化
            int updateIsEnabled = checkIPSwitchUpdae(oldCustVo, newCustVo);
            if (updateIsEnabled <= 1) {
                checkIP.setIsEnabled(updateIsEnabled);
            } else {
                checkIP.setIsEnabled(-1);
            }
            // 具体变化值
            checkIP.setIllegalReqDicision(newCustVo.getCheckOrderIP());
            list.add(checkIP);
            //记日志
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getOrderIpId(), newCustVo);
            log.setValBeforeEdit(getCheckOrderIPName(oldCustVo.getCheckOrderIP()));
            log.setValAfterEdit(getCheckOrderIPName(newCustVo.getCheckOrderIP()));
            log.setEditNature(AntiPhishEditNatureEnum.ILLEGAL_REQ_DICISION.value);
            logList.add(log);
        }
    }
    private String getCheckOrderIPName(String checkOrderIP){
        if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.CLAOSE.value)){
            return "关闭";
        }else if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.REJECT.value)){
            return "拒绝支付";
        }else if(checkOrderIP.equals(AntiPhishCheckOrderIpEnum.WARN.value)){
            return "提示用户";
        }
        return null;
    }

    private void setTimeStampChange(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {
        if (!newCustVo.getCheckTimeStamp().equals(oldCustVo.getCheckTimeStamp())
                || !newCustVo.getLegalValue().equals(oldCustVo.getLegalValue())) {
            
            RcsAntiPhishConfCust checktimeStamp = new RcsAntiPhishConfCust();
            checktimeStamp.setAntiPhishType("03");
            checktimeStamp.setConfSq(Integer.parseInt(oldCustVo.getTimeStampId()));
            // 开关是否变化
            if (!newCustVo.getCheckTimeStamp().equals(oldCustVo.getCheckTimeStamp())) {
                checktimeStamp.setIsEnabled(Integer.parseInt(newCustVo.getCheckTimeStamp()));
                
                //记日志
                RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getTimeStampId(), newCustVo);
                log.setValBeforeEdit(oldCustVo.getCheckTimeStamp().equals("1")?"开启":"关闭");
                log.setValAfterEdit(newCustVo.getCheckTimeStamp().equals("1")?"开启":"关闭");
                log.setEditNature(AntiPhishEditNatureEnum.DISJUNCTOR.value);
                logList.add(log);
            } else {
                checktimeStamp.setIsEnabled(-1);
            }
            // 时间戳值不修改 时间戳写死45
            list.add(checktimeStamp);
        }
    }
    
    private void setDomainLikeChange(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {

        String newCheckLikeDomain = newCustVo.getCheckLikeDomain();
        String oldCheckLikeDomain = StringTools.checkNulltoString(oldCustVo.getCheckLikeDomain());
        String newCheckDomainLikeValue = StringTools.checkNulltoString(newCustVo.getCheckDomainLikeValue());
        String oldCkeckDomainLikeValue = StringTools.checkNulltoString(oldCustVo.getCheckDomainLikeValue());
        // 模糊域名是否发生变化
        boolean checkLikeDomainChange = !newCheckLikeDomain.equals(oldCheckLikeDomain);
        // 域名列表否发生变化
        boolean checkDomainValueChange = !newCheckDomainLikeValue.equals(oldCkeckDomainLikeValue);
        
        RcsAntiPhishConfCust likeDomain = new RcsAntiPhishConfCust();
        likeDomain.setConfSq(Integer.parseInt(oldCustVo.getCheckLikeDomainId()));
        boolean isUpdate=false;
        
        if ( checkLikeDomainChange) {
            // 精确域名是否修改
            likeDomain.setIsEnabled(Integer.parseInt(newCheckLikeDomain));
            isUpdate=true;
            //记日志
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getCheckLikeDomainId(), newCustVo);
            log.setValBeforeEdit(oldCustVo.getCheckLikeDomain().equals("1")?"开启":"关闭");
            log.setValAfterEdit(newCustVo.getCheckLikeDomain().equals("1")?"开启":"关闭");
            log.setEditNature(AntiPhishEditNatureEnum.ANTI_PHISH_TYPE.value);
            logList.add(log);
        }
        if( checkDomainValueChange){
            likeDomain.setIsEnabled(Integer.parseInt(newCheckLikeDomain));
            likeDomain.setLegalValue(newCheckDomainLikeValue);
            isUpdate=true;
            //记日志模糊域名都相应的改变
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getDomainId(), newCustVo);
            log.setValBeforeEdit(oldCustVo.getCheckDomainLikeValue());
            log.setValAfterEdit(newCustVo.getCheckDomainLikeValue());
            log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
            logList.add(log);
        }
        if(isUpdate){
            list.add(likeDomain);
        }
    }
    private void setDomainChange(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {
        String newCheckDomain = newCustVo.getCheckDomain();
        String oldCheckDomain = StringTools.checkNulltoString(oldCustVo.getCheckDomain());
        String newCheckDomainValue = newCustVo.getCheckDomainValue();
        String oldCkeckDomainValue = StringTools.checkNulltoString(oldCustVo.getCheckDomainValue());
        // 域名列表否发生变化
        boolean checkDomainValueChange = !newCheckDomainValue.equals(oldCkeckDomainValue);
        // 精确域名是否发生变化
        boolean checkDomainChange = !newCheckDomain.equals(oldCheckDomain);
        
        RcsAntiPhishConfCust domain = new RcsAntiPhishConfCust();
        domain.setConfSq(Integer.parseInt(oldCustVo.getDomainId()));
        boolean isUpdate=false;
        if (checkDomainChange) {
             domain.setIsEnabled(Integer.parseInt(newCheckDomain));
            isUpdate=true;
            //记日志
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getDomainId(), newCustVo);
            log.setValBeforeEdit(oldCustVo.getCheckDomain().equals("1")?"开启":"关闭");
            log.setValAfterEdit(newCustVo.getCheckDomain().equals("1")?"开启":"关闭");
            log.setEditNature(AntiPhishEditNatureEnum.ANTI_PHISH_TYPE.value);
            logList.add(log);
        }
        if(checkDomainValueChange){
            domain.setLegalValue(newCheckDomainValue);
            domain.setIsEnabled(Integer.parseInt(newCheckDomain));
            isUpdate=true;
            //记日志(精确域名和模糊域名都相应的改变 但是只记一个日志)
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getDomainId(), newCustVo);
            log.setValBeforeEdit(oldCustVo.getCheckDomainValue());
            log.setValAfterEdit(newCustVo.getCheckDomainValue());
            log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
            logList.add(log);
        }
        if(isUpdate){
            list.add(domain);
        }
    }
    
    /**
     * 日志 基础数据
     * @param confSq
     * @param newCustVo
     * @return
     */
    private RcsAntiPhishConfEditLog getCustBaseLog(String confSq,RcsAntiPhishConfCustlVo newCustVo){
        RcsAntiPhishConfEditLog log=new RcsAntiPhishConfEditLog();
        log.setOperConfType("02");
        log.setOperId(newCustVo.getOperId());
        log.setOperName(newCustVo.getOperName());
        log.setOperTargetSq(confSq);
        return log;
        
    }

    /**
     * 设置验证码更新
     * 
     * @param newCustVo
     * @param list
     * @param oldCustVo
     * @param enterValidityCodeSwitch
     */
    private void setEnterValidityCode(RcsAntiPhishConfCustlVo newCustVo, List<RcsAntiPhishConfCust> list,
            RcsAntiPhishConfCustlVo oldCustVo,List< RcsAntiPhishConfEditLog> logList) {

        RcsAntiPhishConfCust checkEnterValidityCode01 = new RcsAntiPhishConfCust();
        checkEnterValidityCode01.setAntiPhishType("05");
        checkEnterValidityCode01.setConfSq(Integer.parseInt(oldCustVo.getEnterValidatiCodeId()));
        // 设置logo
        if (StringUtils.isNotBlank(newCustVo.getUploadLogo())&&
                !newCustVo.getUploadLogo().equals(oldCustVo.getUploadLogo())) {
            checkEnterValidityCode01.setLegalValue(StringTools.checkNulltoString(newCustVo.getUploadLogo()));
            
            //上传log记日志
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getEnterValidatiCodeId(), newCustVo);
            log.setValBeforeEdit(oldCustVo.getUploadLogo());
            log.setValAfterEdit(newCustVo.getUploadLogo());
            log.setEditNature(AntiPhishEditNatureEnum.LEGAL_VALUE.value);
            logList.add(log);
        }
        String nowEnterValidityCode = newCustVo.getEnterValidityCode();
        if (AntiPhishGetwayChannelEnum.ALL_CLOSE.value.equals(nowEnterValidityCode)) {
            checkEnterValidityCode01.setIsEnabled(0);
        } else  {
            checkEnterValidityCode01.setIsEnabled(1);
            checkEnterValidityCode01.setGatewayChannel(nowEnterValidityCode);
        }
        list.add(checkEnterValidityCode01);
        if(!newCustVo.getEnterValidityCode().equals(oldCustVo.getEnterValidityCode())){
            //记日志
            RcsAntiPhishConfEditLog log=getCustBaseLog(oldCustVo.getEnterValidatiCodeId(), newCustVo);
            log.setValBeforeEdit(getEnterValidatinName(oldCustVo.getEnterValidityCode()));
            log.setValAfterEdit(getEnterValidatinName(newCustVo.getEnterValidityCode()));
            log.setEditNature(AntiPhishEditNatureEnum.GATEWAY_CHANNEL.value);
            logList.add(log);
        }
    }
    
    private String getEnterValidatinName(String enterValidatiCode){
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.ALL_CLOSE.value)){
            return "关闭";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.DIRECT_BANK.value)){
            return "直连开启";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.UN_DIRECT_BANK.value)){
            return "非直连开启";
        }
        if(enterValidatiCode.equals(AntiPhishGetwayChannelEnum.ALL_OPEN.value)){
            return "全部开启";
        }
        return null;
    }

    /**
     * 校验IP开关是否更新
     * 
     * @param oldCustVo
     * @param newCustVo
     * @return
     */
    private int checkIPSwitchUpdae(RcsAntiPhishConfCustlVo oldCustVo, RcsAntiPhishConfCustlVo newCustVo) {

        if (oldCustVo.getCheckOrderIP().equals("00") && !newCustVo.getCheckOrderIP().equals("00")) {
            return 1;
        }
        if (!oldCustVo.getCheckOrderIP().equals("00") && newCustVo.getCheckOrderIP().equals("00")) {
            return 0;
        }
        return 2;
    }

    /*
     * 查询指定商户规则VO
     * 
     * @see com.gopay.common.rcs.service.antiphish.manager.
     * RcsAntiPhishingManagerCustService#getCustConfBySql(java.lang.String)
     */
    @Override
    public RcsAntiPhishConfCustlVo getCustConfBySql(String custId) {

        RcsAntiPhishConfCustlVo confVo = new RcsAntiPhishConfCustlVo();

        List<?> strCustConf = rcsAntiPhishingCustManager.getCustConfBySql(custId);
        if (null == strCustConf || strCustConf.isEmpty()) {//未查到配置数据，初始化
        	confVo = rcsAntiPhishingCustManager.getMerchantByCustId(custId);
        } else {
            buildVo(confVo, strCustConf);
        }
        return confVo;
    }

    /**
     * 构建返回单个VO对象
     * 
     * @param confVo
     * @param strCustConf
     */
    private void buildVo(RcsAntiPhishConfCustlVo confVo, List<?> custConfArrayList) {
        boolean isSetBaseInfo = false;
        for (int i = 0, size = null == custConfArrayList ? 0 : custConfArrayList.size(); i < size; i++) {
        	Object[] propertyArray = (Object[])custConfArrayList.get(i);
            // 设置基本公共信息
            if (!isSetBaseInfo) {
                confVo.setCustEmail((String) propertyArray[5]);
                confVo.setCustMob((String) propertyArray[6]);
                confVo.setCustName((String) propertyArray[7]);
                confVo.setSiteName((String) propertyArray[8]);
                confVo.setCustId((String) propertyArray[9]);
            }
            isSetBaseInfo = true;
            // 校验类型
            String antiPhishType = (String) propertyArray[0];
            // 开关
            String isEnabled = ((BigDecimal) propertyArray[1]).toString();
            //当前规则的ID
            String antifishId=((BigDecimal)propertyArray[10]).toString();
            if (antiPhishType.equals("01")) {
                confVo.setCheckDomain(isEnabled);
                confVo.setCheckDomainValue(StringTools.checkNulltoString(propertyArray[2]));
                confVo.setDomainId(antifishId);
            }else if (antiPhishType.equals("02")) {
                confVo.setCheckLikeDomain(isEnabled);
                confVo.setCheckLikeDomainId(antifishId);
                confVo.setCheckDomainLikeValue(StringTools.checkNulltoString(propertyArray[2])); //生产bug7期 lx 2017-07-20 11:10:46
            } else if (antiPhishType.equals("03")) {
                // 时间戳间隔校验
                confVo.setCheckTimeStamp(isEnabled);
                confVo.setLegalValue(StringTools.checkNulltoString(propertyArray[2]));
                confVo.setTimeStampId(antifishId);
            } else if (antiPhishType.equals("04")) {
                // 订单IP校验
                confVo.setCheckOrderIP(getCheckOrderIPVal((String) propertyArray[3], isEnabled));
                confVo.setOrderIpId(antifishId);
            } else if (antiPhishType.equals("05")) {
                // 验证码输入
                String gatewayChannel = (String) propertyArray[4];
                if(isEnabled.equals("0")){
                    gatewayChannel=AntiPhishGetwayChannelEnum.ALL_CLOSE.value;
                }
                confVo.setEnterValidityCode(gatewayChannel);
                confVo.setUploadLogo((String) propertyArray[2]);
                confVo.setEnterValidatiCodeId(antifishId);
            } else if (antiPhishType.equals("06")) {
                confVo.setReqSourceIp((String) propertyArray[2]);
                confVo.setReqSourceIpId(antifishId);
            }
        }
    }


    /**
     * 订单IP校验的值
     * 
     * @param confCust
     * @return
     */
    private String getCheckOrderIPVal(String illegalReqDicision, String isEnabled) {
        String checkOrderIPVal = null;
        if (isEnabled.equals("1")) {
            checkOrderIPVal = illegalReqDicision;
        } else {
            checkOrderIPVal = AntiPhishCheckOrderIpEnum.CLAOSE.value;
        }
        return checkOrderIPVal;
    }

    @Override
    public List<RcsAntiPhishConfCustlVo> getMerchantBySql(String custName, String email, String custMob, int pageNo, int pageSize) {
        return rcsAntiPhishingCustManager.getMerchantBySql(custName, email, custMob, pageNo, pageSize);
    }

    @Override
    public int getMerchantCount(String custName, String custEmail, String custMob) {
        return rcsAntiPhishingCustManager.getMerchantCount(custName, custEmail, custMob);
    }
    @Override
    public void delLogo(String custId,String operId,String operName) {
        rcsAntiPhishingCustManager.delLogo(custId,operId,operName);
        
    }

}
